import {createRouter, createWebHistory} from "vue-router";
import {ElMessage} from "element-plus";
import {useUserStore} from "@/stores/userStores";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: "/",
			name: "/",
			component: () => import("@/views/film/index.vue"),
			redirect: "/home",
			children: [
				{
					path: "/home",
					name: "home",
					component: () => import("@/views/film/home/home.vue"),
				},
				{
					path: "/tags",
					name: "tags",
					component: () => import("@/views/film/tags/tags.vue"),
				},
				{
					path: "/me",
					name: "me",
					component: () => import("@/views/film/me/me.vue"),
				},
				{
					path: "/detail",
					name: "detail",
					component: () => import("@/views/film/detail/detail.vue"),
				},
				{
					path: "/message",
					name: "message",
					component: () => import("@/views/film/message/message.vue"),
				},
				{
					path: "/search",
					name: "search",
					component: () => import("@/views/film/search/search.vue"),
				},
				{
					path: "/hot",
					name: "hot",
					component: () => import("@/views/film/hot/hots.vue"),
				},
			],
		},
		//后台模块
		{
			path: "/admin",
			name: "admin",
			component: () => import("@/views/admin/index.vue"),
			redirect: "/admin/user",
			children: [
				{
					path: "/admin/actor",
					name: "actor",
					component: () => import("@/views/admin/actor/actor.vue"),
				},
				{
					path: "/admin/announce",
					name: "announce",
					component: () => import("@/views/admin/announce/announce.vue"),
				},
				{
					path: "/admin/comment",
					name: "comment",
					component: () => import("@/views/admin/comment/comment.vue"),
				},
				{
					path: "/admin/move",
					name: "move",
					component: () => import("@/views/admin/move/move.vue"),
				},
				{
					path: "/admin/tag",
					name: "tag",
					component: () => import("@/views/admin/tag/tag.vue"),
				},

				{
					path: "/admin/score",
					name: "score",
					component: () => import("@/views/admin/score/score.vue"),
				},
				{
					path: "/admin/user",
					name: "user",
					component: () => import("@/views/admin/user/user.vue"),
				},
			],
		},
		{
			path: "/login",
			component: () => import("@/views/login.vue"),
		},
		{
			path: "/register",
			component: () => import("@/views/register.vue"),
		},
	],
});

const whitelisting = ["/login", "/register"];
router.beforeEach((to, from, next) => {
	const userStore = useUserStore();
	if (userStore.getToken() && to.path.includes("/admin")) {
		const user = userStore.getUserInfo();
		if (user.level === 1) {
			next();
		} else {
			ElMessage.error("无权访问后台系统");
			next({path: "/home"});
		}
	}
	if (!whitelisting.includes(to.path) && !userStore.getToken()) {
		next({path: "/login"});
	} else {
		next();
	}
});

export default router;
